package com.cloudbroker.bsc.common.search.util;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloudbroker.bsc.common.search.common.SearchEngineAdapterClient;

public class SearchTask {
    public static ExecutorService pool;
    
    private static final Logger logger = LoggerFactory.getLogger(SearchTask.class);
    
    static {
        pool = Executors.newFixedThreadPool(10);
    }
    
    public static void deleteSearchTask(final String table_name, final String row_id) {
        pool.execute(new Runnable() {
            
            @Override
            public void run() {
                try {
                    Boolean b = SearchEngineAdapterClient.getSearchEngineAdapter().delete(table_name, row_id);
                    if (b) {
                        logger.info("删除搜索引擎表" + table_name + "，row_id：" + row_id + "成功");
                    }
                } catch (Exception e) {
                    logger.info("删除搜索引擎表" + table_name + "，row_id：" + row_id + "失败", e.getMessage());
                }
            }
        });
    }
    
    public static void deleteBitchSearchTask(final Map<String, String> map) {
        pool.execute(new Runnable() {
            
            @Override
            public void run() {
                try {
                    Boolean b = SearchEngineAdapterClient.getSearchEngineAdapter().delete_query(map);
                    if (b) {
                        logger.info("批量删除成功");
                    }
                } catch (Exception e) {
                    logger.info("批量删除搜索引擎表失败", e.getMessage());
                }
            }
        });
    }
    
    public static void updateBitchTask(final Map mapQry, final Map mapUpdate) {
        pool.execute(new Runnable() {
            
            @Override
            public void run() {
                try {
                    Boolean b = SearchEngineAdapterClient.getSearchEngineAdapter().update_query(mapQry, mapUpdate);
                    if (b) {
                        logger.info("批量更新成功");
                    }
                } catch (Exception e) {
                    logger.info("批量更新搜索引擎失败", e.getMessage());
                }
            }
        });
    }
    
    public ExecutorService getPool() {
        return pool;
    }
}
